<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/10/8
 * Time: 11:23
 */

namespace app\common\library;

use think\Exception;
use think\Request;

class Redis {
	
	protected static $instance;
	protected $error;
	
	protected function __construct(){
		try{
			if(!extension_loaded('redis')){
				throw new Exception('redis扩展不存在');
			}
			
			$redis = new \Redis();
			$config = config('redis');
			$redis->connect($config['host'], $config['port'],$config['timeout']);
			$config['password'] && $redis->auth($config['password']);
			$redis->select($config['database']);
			
			self::$instance = $redis;
		} catch (Exception $e) {
			echo $e->getMessage();die;
			$this->error = $e->getMessage();
			self::$instance = false;
		}
	}
	
	public function getError() {
		return $this->error;
	}
	
	public static function  getInstance() {
		if(!self::$instance) {
			new self();
		}
		
		return self::$instance;
	}
	
	
	public function __call($name, $arguments)
	{
		return self::$instance->$name(...$arguments);
	}

	public static function getPrefixKey($key) {
		return config('redis.prefix').$key;
	}
}
